package service

import (
	"bytes"
	"fmt"
	"io"
	"net/http"

	"github.com/PuerkitoBio/goquery"
	"github.com/axgle/mahonia"
)

func GetList(url string) {

	client := http.Client{}

	req, err := http.NewRequest("GET", "https://www.ddyucshu.cc/20221_20221439/", nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
	req.Header.Set("Accept", "text/html;charset=UTF-8")

	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	defer resp.Body.Close()
	content, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	decoder := mahonia.NewDecoder("GBK")
	utf8content := decoder.ConvertString(string(content))
	fmt.Println(utf8content)
	doc, err := goquery.NewDocumentFromReader(bytes.NewReader([]byte(utf8content)))
	if err != nil {
		panic(err)
	}

	doc.Find("div#list dl dd").Each(func(i int, s *goquery.Selection) {
		if i > 5 {
			href, _ := s.Find("a").Attr("href")
			name := s.Find("a").Text()
			println(i)
			println(href)
			println(name)
		}
	})
}
